//
//  MultipleShowView.m
//  YSSmartHome
//
//  Created by Yahaong on 16/8/12.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "MultipleShowView.h"
#import "ZTDevice.h"
#import "ZTOneControlDevice.h"
#import "ZTTwoControlDevice.h"
#import "ZTThreeControlDevice.h"
#import "ZTFourControlDevice.h"
#import "YHMarcros.h"
#import "UIView+Frame.h"
#import "XAIGateway.h"

static const CGFloat kButtonDefaulOffset = 8.0f;
@interface MultipleShowView ()

@property(nonatomic, strong)NSMutableArray <UIButton *> *arryM;
@end
@implementation MultipleShowView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.arryM = [NSMutableArray arrayWithCapacity:4];
        for (NSInteger i = 0; i < 4; i++) {
            UIButton *button = [[UIButton alloc]init];
            [button setImage:[UIImage imageNamed:@"statusClose"] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"statusOpen"] forState:UIControlStateSelected];
            [self addSubview:button];
            UIButton* lastButton = [self.arryM lastObject];
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.equalTo(lastButton ? lastButton.trailing : self);
                make.top.bottom.equalTo(self);
                make.width.equalTo(self.height);
            }];
            [self.arryM addObject:button];
        }
    }
    return self;
}

- (void)setDevice:(ZTDevice *)device
{
    _device = device;
    [self.arryM  makeObjectsPerformSelector:@selector(setSelected:) withObject:@(NO)];
    [self.arryM  makeObjectsPerformSelector:@selector(setHidden:) withObject:@(NO)];
    if (device.devType == ZTDeviceTypeOneWay0Switch
        || device.devType == ZTDeviceTypeOneWaySwitch)
    {
        [self.arryM[1] setHidden:YES];
        [self.arryM[2] setHidden:YES];
        [self.arryM[3] setHidden:YES];
        UIButton* firstButton = self.arryM[0];
        [firstButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self).offset(kButtonDefaulOffset *1.5);
        }];
        [self layoutIfNeeded];
        ZTOneControlDevice *oneDevice = (ZTOneControlDevice *)device;
        if (device.devStatus == ZTDeviceStatusOFFLINE || kMQTTSessionManager.currentGateway.devStatus == ZTDeviceStatusOFFLINE) {
            [self.arryM[0] setSelected:false];
        }
        else
        {
            [self.arryM[0] setSelected:oneDevice.status];
        }
      
       
    }
    else if (device.devType == ZTDeviceTypeTwoWay0Switch
             || device.devType == ZTDeviceTypeTwoWaySwitch
             || device.devType == ZTDeviceTypeCurtainDoubleTrack)
    {
        
        [self.arryM[3] setHidden:YES];
        [self.arryM[2] setHidden:YES];
        UIButton* firstButton = self.arryM[0];
        [firstButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self).offset(kButtonDefaulOffset);
        }];
        [self layoutIfNeeded];
        ZTTwoControlDevice *switchDevice = (ZTTwoControlDevice *)device;
        if (device.devStatus == ZTDeviceStatusOFFLINE || kMQTTSessionManager.currentGateway.devStatus == ZTDeviceStatusOFFLINE) {
            [self.arryM[0] setSelected:false];
            [self.arryM[1] setSelected:false];
        }
        else
        {
            [self.arryM[0] setSelected:switchDevice.oneStatus];
            [self.arryM[1] setSelected:switchDevice.twoStatus];
        }
       
    }
    else if (device.devType == ZTDeviceTypeThreeWay0Switch
             || device.devType == ZTDeviceTypeThreeWaySwitch)
    {
        [self.arryM[3] setHidden:YES];
        UIButton* firstButton = self.arryM[0];
        [firstButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self).offset(kButtonDefaulOffset * 0.5);
        }];
        [self layoutIfNeeded];
        ZTThreeControlDevice *switchDevice = (ZTThreeControlDevice *)device;
        if (device.devStatus == ZTDeviceStatusOFFLINE || kMQTTSessionManager.currentGateway.devStatus == ZTDeviceStatusOFFLINE) {
            [self.arryM[0] setSelected:false];
            [self.arryM[1] setSelected:false];
            [self.arryM[2] setSelected:false];
        }
        else
        {
            [self.arryM[0] setSelected:switchDevice.oneStatus];
            [self.arryM[1] setSelected:switchDevice.twoStatus];
            [self.arryM[2] setSelected:switchDevice.threeStatus];
        }
        
    }
    if (device.devType == ZTDeviceTypeFourWay0Switch
        || device.devType == ZTDeviceTypeFourWaySwitch)
    {
        UIButton* firstButton = self.arryM[0];
        [firstButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self);
        }];
        [self layoutIfNeeded];
        ZTFourControlDevice *switchDevice = (ZTFourControlDevice *)device;
        if (device.devStatus == ZTDeviceStatusOFFLINE || kMQTTSessionManager.currentGateway.devStatus == ZTDeviceStatusOFFLINE) {
            [self.arryM[0] setSelected:false];
            [self.arryM[1] setSelected:false];
            [self.arryM[2] setSelected:false];
            [self.arryM[3] setSelected:false];
        }
        else
        {
            [self.arryM[0] setSelected:switchDevice.oneStatus];
            [self.arryM[1] setSelected:switchDevice.twoStatus];
            [self.arryM[2] setSelected:switchDevice.threeStatus];
            [self.arryM[3] setSelected:switchDevice.fourStatus];
        }
        
    }
    else if (device.devType == ZTDeviceTypeOneWaySocket)
    {
        [self.arryM[1] setHidden:YES];
        [self.arryM[2] setHidden:YES];
        [self.arryM[3] setHidden:YES];
        UIButton* firstButton = self.arryM[0];
        [firstButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self).offset(kButtonDefaulOffset *1.5);
        }];
        [self layoutIfNeeded];
        ZTOneControlDevice *socketDevice = (ZTOneControlDevice *)device;
        if (device.devStatus == ZTDeviceStatusOFFLINE || kMQTTSessionManager.currentGateway.devStatus == ZTDeviceStatusOFFLINE) {
            [self.arryM[0] setSelected:false];
        }
        else
        {
          [self.arryM[0] setSelected:socketDevice.status];
        }
    }
}
@end
